package cn.picclife.mwx.manager.sys.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.picclife.mwx.manager.sys.mapper.RolesMapper;
import cn.picclife.mwx.manager.sys.model.bo.CommonBo;
import cn.picclife.mwx.manager.sys.model.bo.PageCount;
import cn.picclife.mwx.manager.sys.model.bo.RolesBo;
import cn.picclife.mwx.manager.sys.model.po.Enum;
import cn.picclife.mwx.manager.sys.service.RolesMgMtService;
import cn.picclife.mwx.manager.util.uniqueseq.UniqueSeq;

@Service
public class RolesMgMtServiceImpl implements RolesMgMtService {
	@Autowired
	private RolesMapper rolesMapper;

	@Override
	public int insertRoles(RolesBo rolesBo) {
		Long serno = UniqueSeq.getUniqueSeq("role_id", "t_roles");
		rolesBo.setRoleId(serno);
		rolesBo.setCrtDate(new Date());
		rolesBo.setDataFlag("1");
		return rolesMapper.insertRoles(rolesBo);
	}

	@Override
	public RolesBo queryModifyRoles(String roleNo) {
		return rolesMapper.queryCountRoles(roleNo);
	}

	@Override
	public int updateRoles(RolesBo rolesBo) {
		rolesBo.setMdfDate(new Date());
		return rolesMapper.updateRoles(rolesBo);
	}

	@Override
	public PageCount queryRolesPage(PageCount pageCount) {
		// RolesBo roleBo = (RolesBo)pageCount.getObject();
		int allCount = rolesMapper.queryRolesAllCount(pageCount.getObject());
		int allPages = allCount / pageCount.getRowNum();

		if (allCount % pageCount.getRowNum() > 0) {
			allPages += 1;
		}

		pageCount.setAllPages(allPages);
		pageCount.setAllCount(allCount);
		int minTotal = (pageCount.getNowPage() - 1) * pageCount.getRowNum();
		int maxTotal = pageCount.getNowPage() * pageCount.getRowNum();
		pageCount.setMinTotal(minTotal);
		pageCount.setMaxTotal(maxTotal);
		pageCount.setMaxminTotal(maxTotal-minTotal);
		List<RolesBo> rolesList = rolesMapper.queryRolesList(pageCount);
		pageCount.setDatas(rolesList);
		return pageCount;
	}

	@Override
	public List<Map<String, Object>> queryRolesMenu(RolesBo rolesBo) {
		Long roleId=rolesMapper.queryRoleId(rolesBo.getRoleNo());
		rolesBo.setRoleId(roleId);
		rolesBo.setObjectNo(rolesBo.getRoleNo());
		return rolesMapper.queryRolesMenu(rolesBo);
	}

	@Override
	public PageCount queryRolesUser(PageCount pageCount) {
		String roleId=rolesMapper.queryOptroId(pageCount.getObject().get("roleNo"));
		pageCount.getObject().put("roleId", roleId);
		int allCount = rolesMapper.queryRolesUserCount(pageCount.getObject());
		int allPages = allCount / pageCount.getRowNum();

		if (allCount % pageCount.getRowNum() > 0) {
			allPages += 1;
		}
		pageCount.setAllPages(allPages);
		pageCount.setAllCount(allCount);
		int minTotal = (pageCount.getNowPage() - 1) * pageCount.getRowNum();
		int maxTotal = pageCount.getNowPage() * pageCount.getRowNum();
		pageCount.setMinTotal(minTotal);
		pageCount.setMaxTotal(maxTotal);
		pageCount.setMaxminTotal(maxTotal-minTotal);
		List<Map<String, Object>> rolesList = rolesMapper.queryRolesUser(pageCount);
		pageCount.setDatas(rolesList);
		return pageCount;
	}

	@Override
	public RolesBo queryCountRoles(String roleNo) {
		return rolesMapper.queryCountRoles(roleNo);
	}

	@Override
	public int saveRolesMenu(CommonBo commonBo, String user) {
		int num = 0;
		num=rolesMapper.deleteRolesMenu(commonBo.getObject().get("roleNo").toString());	
		if (null == commonBo.getDatas() || 0 == commonBo.getDatas().size()) {
          return num;
		} else {
		for (Map<String, Object> rolesUser : commonBo.getDatas()) {
			if(rolesUser.get("chkDisabled").equals("false")){
				RolesBo rolesMenuBo = new RolesBo();
				Long serno = UniqueSeq.getUniqueSeq("auth_id", "t_data_auth");
				rolesMenuBo.setRoleNo(commonBo.getObject().get("roleNo").toString());
				rolesMenuBo.setDataAuthNo(rolesUser.get("dataAuthNo").toString());
				rolesMenuBo.setCrtDate(new Date());
				rolesMenuBo.setRoleId(serno);
				rolesMenuBo.setDataFlag("1");
				rolesMenuBo.setObjectType("ROLE");
				rolesMenuBo.setDataAuthType("MENU");
				rolesMenuBo.setCrtUser(user);
				rolesMenuBo.setStartDate(new Date());
				num = rolesMapper.saveRolesMenu(rolesMenuBo);
			}
		}
		}
		return num;
	}

	@Override
	public int saveRolesUser(CommonBo commonBo, String user) {
		int num = 0;

		for (Map<String, Object> rolesUser : commonBo.getDatas()) {
			RolesBo rolesUserBo = new RolesBo();
			Long serno = UniqueSeq.getUniqueSeq("optro_id", "t_operator_roles");
			rolesUserBo.setRoleNo(commonBo.getObject().get("roleNo").toString());
			rolesUserBo.setOptNo(rolesUser.get("optNo").toString());
			rolesUserBo.setCrtDate(new Date());
			rolesUserBo.setRoleId(serno);
			rolesUserBo.setCrtUser(user);
			rolesUserBo.setDataFlag("1");
			rolesUserBo.setStartDate(new Date());
			num += rolesMapper.deleteRolesUser(rolesUserBo);
			if ("1".equals(rolesUser.get("checked").toString())|| "true".equals(rolesUser.get("checked").toString())) {
				num += rolesMapper.saveRolesUser(rolesUserBo);
			}
		}
		return num;
	}

	@Override
	public List<Enum> queryRoleType() {
		return rolesMapper.queryRoleType();
	}

	@Override
	public List<Enum> queryAgentCateType() {
		return rolesMapper.queryAgentCateType();
	}
/*
 * 
 * 删除角色
	*/
	@Override
	public int deleteUser(HttpServletRequest req, HttpServletResponse res, CommonBo commonBo) {
		/*operatorMapper.deleteOperatorRoles(commonBo);
		operatorMapper.deleteUserBranchs(commonBo);
		int row= operatorMapper.deleteByPrimaryKey(commonBo);*/
		
		
	 int num = 	rolesMapper.deleteRolesOperator(commonBo);
	  int num2 = 	rolesMapper.deleteRolesAuth(commonBo);
		int row= rolesMapper.deleteByPrimaryKeyPro(commonBo);
		
		
		return row;
	}

}
